package drds.configuration.web.build_model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


public class Model {

    public static Connection getConnection() throws Exception {
        Class.forName("org.mariadb.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/drds_home", "root", "root");
        return connection;
    }


    public static List<String> getTableNameList() throws Exception {
        List<String> tableNameList = new ArrayList<String>();
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("SHOW TABLES ");
        while (resultSet.next()) {
            String tableName = resultSet.getString(1);
            tableNameList.add(tableName);
        }
        resultSet.close();
        statement.close();
        connection.close();
        return tableNameList;
    }


    public static void getColumnNameListByTableName(String folderPath, String groupName, List<String> tableNameList)
            throws Exception {

        for (int i = 0; i < tableNameList.size(); i++) {
            table(folderPath, groupName, tableNameList.get(i));

        }

    }

    private static void table(String folderPath, String groupName, String tableName) throws Exception, SQLException {

        String newLine = "\n";
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("show full columns from " + tableName);
        System.out.println("=========================================================================");
        System.out.println("tableName[" + tableName + "]");
        String tableNameToModel = ColumnToPropertyUtil.columnToProperty(tableName);
        String s0 = String.valueOf(tableName.charAt(0));
        String s1 = s0.toUpperCase();
        tableNameToModel = tableNameToModel.replaceFirst(s0, s1);
        String tableModel = tableNameToModel + "BaseModel";

        FileWriter fileWriter = new FileWriter(folderPath + File.separator + tableModel + ".java", true);
        BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

        bufferedWriter.write("package tool.plus.web.table_model." + groupName + ";");
        bufferedWriter.write(newLine);
        //
        bufferedWriter.write("import Model;");
        bufferedWriter.write(newLine);
        bufferedWriter.write("import Table;");
        bufferedWriter.write(newLine);
        bufferedWriter.write("import Objects;");
        bufferedWriter.write(newLine);
        bufferedWriter.write("import GroupDataSourceId;");
        bufferedWriter.write(newLine);
        bufferedWriter.write("@Table(groupDataSourceId = GroupDataSourceId." + groupName.toUpperCase()
                + ", tableName = \"" + tableName + "\", primaryKey = \"id\")");
        bufferedWriter.write(newLine);
        bufferedWriter.write("public class " + tableModel + " extends Model {");
        bufferedWriter.write(newLine);
        bufferedWriter.write("public static " + tableModel + " dao = new " + tableModel + "();");

        bufferedWriter.write(newLine);

        bufferedWriter.write("public static final String __TABLE_NAME__ = \"" + tableName + "\";");
        bufferedWriter.write(newLine);

        while (resultSet.next()) {
            bufferedWriter.write(newLine);
            bufferedWriter.write("public static final String " + resultSet.getString("Field").toUpperCase() + " = \""
                    + resultSet.getString("Field") + "\";");
        }

        bufferedWriter.write(newLine);
        bufferedWriter.write("}");

        bufferedWriter.close();
        bufferedWriter.close();

        resultSet.close();
        statement.close();
        connection.close();

    }

    /**
     * <pre>
     * package tool.plus.web.table_model.drds_home;
     *
     * import Model;
     * import Table;
     * import Objects;
     * import GroupDataSourceId;
     *
     * import java.util.List;
     *
     * &#64;Table(groupDataSourceId = GroupDataSourceId.DRDS_HOME, tableName = "table_business_type", primaryKey = "id")
     * public class TableBusinessTypeModel extends Model {
     * 	public static final String TABLE_NAME = "table_business_type";
     * 	//
     * 	public static final String ID = "id";
     * 	public static final String NAME = "name";
     * 	public static final String REMARKS = "remarks";
     * 	public static TableBusinessTypeModel dao = new TableBusinessTypeModel();
     *
     * }
     *
     * </pre>
     */
    public static void main(String[] args) throws Exception {
        String bashPath = "E:\\drds_home\\";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String now = sdf.format(new Date());
        System.out.println("===" + bashPath + now + "===");
        String folderPath = bashPath + now;
        File file = new File(folderPath);
        if (file.exists()) {
            TimeUnit.SECONDS.sleep(1);
            now = sdf.format(new Date());
            file = new File(folderPath);
        }
        file.mkdirs();
        String groupName = "drds_home";
        List<String> tableNameList = getTableNameList();
        getColumnNameListByTableName(folderPath, groupName, tableNameList);

    }
}